package com.example.model;

import java.util.ArrayList;
import java.util.List;

/**
 * This class will hold a collection of all users registered for use with the
 * WheresMyStuff app. It will be used to retrieve users already on the users, or
 * to make sure that a user name is not in use already when someone creates a
 * new user account.
 * 
 * @author 3Factorial
 * @version 1.0
 */
public class UserList {

	private static UserList instance = null;
	private static ArrayList<User> users = new ArrayList<User>();

	/**
	 * Obtains the instance of the user list used for the program.
	 * 
	 * @return The current user list.
	 */
	public static UserList getInstance() {
		if (instance == null) {
			instance = new UserList();
		}
		return instance;
	}

	/**
	 * Instantiates the UserLists.
	 */
	static {
		User def = new User("username", "password");
		User defAdmin = new User("admin", "admin");
		defAdmin.setIsAdmin(true);
		users.add(def);
		users.add(defAdmin);
	} // End constructor

	/**
	 * Adds the given user to the UserLists.
	 * 
	 * @param u
	 *            The user to add to the UserLists.
	 */
	public void add(User u) {
		users.add(u);
	} // End add method

	public static void delete(User u) {
		users.remove(u);
	}

	/**
	 * Determines if the following username and password combination produce a
	 * loginMatch.
	 * 
	 * @param username
	 *            Username to check.
	 * @param password
	 *            Password to match to username.
	 * @return True if there is a match, false if not.
	 */
	public boolean findLoginMatch(String username, String password) {
		// TODO This method needs to exist outside this class. It's considered
		// business logic, so it cannot be here.
		for (int i = 0; i < users.size(); i++) {
			if (users.get(i).getUserName().equals(username)
					&& users.get(i).getPassword().equals(password))
				return true;
		}
		return false;
	} // End findLoginMatch method

	/**
	 * Retrieves a user by finding their username.
	 * 
	 * @param username
	 *            The username to match.
	 * @return The user who matches the username.
	 */
	public User getUserByUsername(String username) {
		for (int i = 0; i < users.size(); i++) {
			if (users.get(i).getUserName().equals(username))
				return users.get(i);
		}
		return null;
	} // End getUserByUsername method

	/**
	 * Determines whether the current username is available to use for a new
	 * user.
	 * 
	 * @param UserName
	 *            The username to determine availability.
	 * @return True if name is available, false if not.
	 */
	public boolean userNameAvailable(String UserName) {
		for (int i = 0; i < users.size(); i++) {
			if (users.get(i).getUserName().equals(UserName)) {
				return false;
			}
		}
		return true;
	} // End userNameAvailable method

	// TODO location?
	/**
	 * Creates a string array with all the usernames registered with the
	 * program.
	 * 
	 * @return a string array with all the usernames registered with the
	 *         program.
	 */
	public static String[] getUsernameList() {
		String[] list = new String[users.size()];
		for (int i = 0; i < users.size(); i++) {
			list[i] = users.get(i).getUserName();
		}
		return list;
	}

} // End UserList class